package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : SetPasswdActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月10日 上午1:00:56
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {}
 *
 */
@EActivity(R.layout.password)
public class SetPasswdActivity extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	EditText passwd_edit;
	@ViewById
	EditText repasswd_edit;
	@ViewById
	Button sumbit_butt;
	
	@AfterViews
	public void initView(){
		topBar.setTopBarClickListener(new TopBarClickListener() {
			
			@Override
			public void rightBtnClick() {
				
			}
			
			@Override
			public void leftBtnClick() {
				onBackPressed();
			}
		});
	}
	
 	@Click({R.id.sumbit_butt})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.sumbit_butt:
			String passwd=passwd_edit.getText().toString().trim();
			String repasswd=repasswd_edit.getText().toString().trim();
			if(TextUtils.isEmpty(passwd) |TextUtils.isEmpty(repasswd)){
				ToastUtils.show(context, "密码不能为空!");
				return;
			}else if(!passwd.equals(repasswd)){
				ToastUtils.show(context, "两次密码不一致!");
				return;
			}
			
			//发送登录请求
			RequestParams params=new RequestParams();
             params.put("password", passwd);
			AsyncHttpUtil.post(URLConfigs.SetPasswd_ukey+myprefs.Ukey().get(), params, SetPasswdResponseHandler);
			break;
		default:
			break;
		}
	}
 	
	//请求响应监听
	TextHttpResponseHandler SetPasswdResponseHandler =new TextHttpResponseHandler() {
		@Override
		public void onSuccess(int statusCode, Header[] arg1, String json) {
			if(statusCode==200){
				LogUtil.e(TAG, "RegisterActivity json :"+json);
				RespondBaseEntity res=GsonUtil.GetFromJson(json, RespondBaseEntity.class);
				if(res.isStatus()){
					//跳转
					openActivity(LoginActivity_.class);
					SetPasswdActivity.this.finish();
				}else{
					ToastUtils.show(context, res.getMsg());
				}
			}
		}
		
		@Override
		public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
			ToastUtils.show(context, R.string.login_fail);
		}
	};
}
